var welId = '';

$(function () {
  if (!isIE8){
    var iframe = document.createElement('iframe');
    iframe.setAttribute("id", "data_Iframe");
    iframe.setAttribute("class", "iframe-flowdata");
    iframe.setAttribute("src", "/scabpm/agilebpm-ui/bpm/definition/bpmData.html");
    $('#flow_iframe').append(iframe);
  }
  getMyMenu()
  // 拖拽到新增标签
  $('#new_target').on("dragover", function (e) {
    e.preventDefault();
  });
  // 拖拽新增
  $('#new_target').on("drop", function (e) {
    e.stopPropagation();
    e.preventDefault();
    drop();

  });
  // 删除快捷菜单
  $(document).on("click", '.expressClose', function (e) {
    deleteWel(e);

  });
  // 快捷菜单打开页面
  $(document).on("click", '.openMenu', function (e) {
    openTab(e, $(this));

  });
  // 拖拽-拖 事件
  $(document).on("dragover", '.menu_li', function (e) {
    e.preventDefault();
  });
  var sourceWelId = '';
  var sourceSort = '';
  // 拖拽开始
  $(document).on("dragstart", '.menu_li', function (e) {
    sourceWelId = $(this).attr("welId");
    sourceSort = $(this).attr('welIndex');
  });
  // 拖拽-鼠标放
  $(document).on("drop", '.menu_li', function (e) {
    if (sourceWelId == null || sourceWelId == '' || sourceWelId == undefined) {
      return;
    }
    var welId = $(this).attr("welId");
    var sort = $(this).attr('welIndex');
    var obj = {
      sourceWelId: sourceWelId,
      targetWelId: welId,
      sourceSort: sort,
      targetSort: sourceSort
    };
    $.post(ctx + "admin/welcome/changePoint", {
      params: JSON.stringify(obj)
    }, function (data) {
      if (data.data)
        $.modal.reload();
      else
        $.modal.alertError(data.msg);
    });
    welId = '';
  });

});

function openTab(e, that) {
  var url = that.attr('path');
  var label = that.attr('label');
  $.modal.openTab(label, url);
}

function  drop() {
  var st = window.sessionStorage.getItem("targetMenu");
  if (st == null || st == undefined || st == '') {
    return;
  }
  var obj = JSON.parse(st);
  // console.log('obj',obj)
  obj.userId = JSON.parse(session.get('userInfo')).userId

  $.ajax({
    url: api + "/admin/syswelcome",
    data: JSON.stringify(obj),
    type: 'post',
    headers: {
      'content-Type': 'application/json;charset=UTF-8'
    },
  }).done(function (res) {
    if (res.data) {
//			$.modal.reload();
      window.location.reload();
      // parent.location.reload()
    } else {
      $.modal.alertError(data.msg);
    }
  })
  window.sessionStorage.removeItem("targetMenu");
}

function deleteWel(e) {
  var welId = e.target.id;
  $.operate.del(api + "/admin/syswelcome/" + welId,  function (data) {
    if (data.data) {
      window.location.reload();
    } else {
      $.modal.alertError(data.msg);
    }

  });
}

function getMyMenu() {
  $.ajax({
    url: api + '/admin/syswelcome/findUserWelcome/' + JSON.parse(session.get('userInfo')).userId,
    type: 'get',
  }).done(function (res) {
    renderTpl('myMenuTpl', {list: res.data}, 'myMenuBox')
    //ie8
    if (isIE8) {
      $('.index_bg .mainmenu li:nth-child(8n+1)').css('backgroundColor', '#36A1DB')
      $('.index_bg .mainmenu li:nth-child(8n+2)').css('backgroundColor', '#678ce1')
      $('.index_bg .mainmenu li:nth-child(8n+3)').css('backgroundColor', '#8c67df')
      $('.index_bg .mainmenu li:nth-child(8n+4)').css('backgroundColor', '#84d018')
      $('.index_bg .mainmenu li:nth-child(8n+5)').css('backgroundColor', '#14c760')
      $('.index_bg .mainmenu li:nth-child(8n+6)').css('backgroundColor', '#f3b613')
      $('.index_bg .mainmenu li:nth-child(8n+7)').css('backgroundColor', '#ff8a4a')
      $('.index_bg .mainmenu li:nth-child(8n+8)').css('backgroundColor', '#fc5366')
    }
  })
}